import sys
from PySide6.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget
from PySide6.QtCore import Qt

class DictionaryTree(QWidget):
    def __init__(self, data):
        super().__init__()

        remote testii

        self.tree_widget = QTreeWidget()
        for key, value in data.items():
            # Create a new QTreeWidgetItem for each key-value pair
            item = QTreeWidgetItem(parent, [str(key), self.format_value(value)])
            item.setFlags(item.flags() | Qt.ItemIsEditable)  # 设置为可编辑

        self.tree_widget.setHeaderLabels(["Key", "Value"])
        self.tree_widget.setHeaderLabels(["Key", "Value"])
        self.tree_widget.setHeaderLabels(["Key", "Value"])
        layout = QVBoxLayout()
        self.tree_widget.itemChanged.connect(self.on_item_changed)  # 连接编辑信号

        self.populate_tree(data)

        layout = QVBoxLayout()
        layout.addWidget(self.tree_widget)
        self.setLayout(layout)

    def populate_tree(self, data, parent=None):
        for key, value in data.items():
            # Create a new QTreeWidgetItem for each key-value pair
            item = QTreeWidgetItem(parent, [str(key), self.format_value(value)])
            item.setFlags(item.flags() | Qt.ItemIsEditable)  # 设置为可编辑

            # Add the item to the tree_widget if there's no parent
            if parent is None:
                self.tree_widget.addTopLevelItem(item)

            # If the value is a dictionary, populate recursively
            if isinstance(value, dict):
                self.populate_tree(value, item)
            # If the value is a list, add each item in the list as a child
            elif isinstance(value, list):
                for v in value:
                    list_item = QTreeWidgetItem(item, ["", self.format_value(v)])  # Key can be empty for list items
                    list_item.setFlags(list_item.flags() | Qt.ItemIsEditable)
                    item.addChild(list_item)  # Add the list item as a child of the current item

    def format_value(self, value):
        # Format the value for display, handling dict and list types
        if isinstance(value, dict) or isinstance(value, list):
            return str(value)  # Display complex types as strings
        return str(value)  # Return the value as a string

    def on_item_changed(self, item, column):
        # 当项目被编辑时触发
        if column == 0:  # 如果是键被编辑
            print(f"Key changed to: {item.text(0)}")
        elif column == 1:  # 如果是值被编辑
            print(f"Value changed to: {item.text(1)}")
            # 在这里可以添加逻辑来更新原始字典数据
            # 例如，您可以通过查找父项来更新字典

if __name__ == "__main__":
    app = QApplication(sys.argv)

    sample_data = {
        "version": "1.0",
        "description": "WRP环境设置文件",
        "SPACE_DOMAINS": "WRP.BUILD",
        "SPACE_TAGS": [
            "WRP_SHARE_TAG",
            "WRP_SHARE_TAG2"
        ],
        "LOCAL_SPACE_TAGS": [
            "WRP_LOCAL_TAG",
            "WRP_LOCAL_TAG2"
        ],
        "LOCAL_REMARK": "本机备注, 不用分享",
        "wrp_info": {
            "macro_prefix": "wrp"
        },
        "wfc": {
            "macro_prefix": "wfc",
            "com_prj_root": "E:/COMPONENT",
            "com_prj_top": "E:/COM_TOP",
            "app_prj_top": "E:/WFC_APP",
            "app_prj_root": "E:/WFC_APP",
            "main_prj_dir": "${wfc.com_prj_root}/WFC4_MAIN",
            "混合仓_prj_dir": "${wfc.com_prj_root}/WFC4_混合仓",
            "kv_ma_dir": "${wfc.app_prj_root}/KV_MA"
        },
        "lang_module": {
            "python": {
                "version": "3.11",
                "venv": ".\\venv",
                "environment": ".\\environment.yml"
            }
        }
    }

    window = DictionaryTree(sample_data)
    window.setWindowTitle("Dictionary Tree Example")
    window.resize(600, 400)
    window.show()

    sys.exit(app.exec())




